<template>
  <lc-sf moda="edit" ref="sf" :schema="record.schema" :data="i || {}" />
  <div class="dialog-footer">
    <el-button
      @click="
        helper.close();
        sf.reset();
      "
      >取消</el-button
    >
    <el-button type="primary" :disabled="!vaild" @click="save()"
      >保存</el-button
    >
  </div>
</template>
<script setup lang="ts" name="add-update-drawer">
import { Drawer } from "@/components/model";
import LcSf from "@/components/sf/sf.vue";
import { Http } from "@/core/api/http.service";
import { Helper } from "@/core/helper/helper.service";
import { Subject } from "rxjs";
import { computed, ref, toRaw } from "vue";
import { NzSafeAny, SFInstance } from "../sf/type";
import { TRecord } from "./interface";
const props = defineProps<{
  helper: Drawer;
  openSub: Subject<null>;
  record: TRecord;
  i: Record<string, NzSafeAny>;
}>();
const sf = ref<SFInstance>();
const vaild = computed(() => {
  if (!sf.value?.service) {
    return true;
  }
  return sf.value?.service.vaild;
});

const formatUrl = () => {
  return props.record.url.split("?")[0];
};
const save = () => {
  const res = sf.value.service.getValues();
  const val = props.record?.format ? props.record?.format(toRaw(res)) : res;
  if (props.i?.id) {
    Http.of()
      .patch(`${formatUrl()}/${props.i?.id}`, val)
      .then(() => {
        Helper.showMessage("数据更新成功");
        close(sf.value.service.getValues(), true);
      });
  } else {
    Http.of()
      .post(`${formatUrl()}`, val)
      .then(() => {
        Helper.showMessage("数据添加成功");
        sf.value.reset();
        close(sf.value.service.getValues(), true);
      });
  }
};
const close = (val: Record<string, NzSafeAny>, destory?: boolean) => {
  props.helper.close(val, destory);
};
props.openSub.subscribe(r => {
  console.log("监听呢---》", r);
});
</script>
./interfaces ../sf/types
